﻿namespace WebApi.Core.Model.ResponseModels
{
    public class ApiResponse<T>
    { 
        public int Code { get; set; } 
        public bool IsSuccess { get; set; }
        public string Message { get; set; }
        public T Data { get; set; }


        // ================= 成功响应快捷方法 =================
        public static ApiResponse<T> Success(T data) => new ApiResponse<T> { Code = 200, IsSuccess = true, Data = data }; 
        public static ApiResponse<T> Success(T data, string message) => new ApiResponse<T> { Code = 200, IsSuccess = true, Data = data, Message = message };

        // ================= 分页响应快捷方法 =================
        public static ApiResponse<PageResult<T>> Page( List<T> items, int pageIndex, int pageSize, int totalCount, string message = "查询成功")
        {
            var pageResult = new PageResult<T>(pageIndex, pageSize, totalCount, items);
            return new ApiResponse<PageResult<T>>
            {
                Code = 200,
                IsSuccess = true,
                Data = pageResult,
                Message = message
            };
        }

        // ================= 错误响应快捷方法 =================
        public static ApiResponse<T> Fail(string message = "操作失败") => new ApiResponse<T> { Code = 500, IsSuccess = false, Message = message }; 
        public static ApiResponse<T> Fail(int code = 500, string message = "操作失败")  => new ApiResponse<T> { Code = code, IsSuccess = false, Message = message };

        
        // ================= 辅助方法 =================
        public static string GetDefaultUserMessage(int code) => code switch
        {
            400 => "请求参数错误",
            401 => "未授权访问",
            403 => "禁止访问",
            404 => "资源不存在",
            500 => "服务器内部错误",
            _ => "操作失败"
        };
    }

    // 非泛型版本用于无数据返回的场景
    public class ApiResponse : ApiResponse<object>
    {
        // ================= 成功响应快捷方法 =================
        public static ApiResponse Success() => new ApiResponse { Code = 200, IsSuccess = true, Message = "操作成功" }; 
        public static ApiResponse Success(string message) => new ApiResponse { Code = 200, IsSuccess = true, Message = message };

        // ================= 错误响应快捷方法 =================
        public new static ApiResponse Fail(string message = "操作失败") => new ApiResponse { Code = 500, IsSuccess = false, Message = message };
        public new static ApiResponse Fail(int code = 500, string message = "操作失败") => new ApiResponse { Code = code, IsSuccess = false, Message = message };
    }

    // 分页结果封装
    public class PageResult<T>
    {
        public int PageIndex { get; }
        public int PageSize { get; }
        public int TotalCount { get; }
        public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
        public List<T> Items { get; }

        public PageResult(int pageIndex, int pageSize, int totalCount, List<T> items)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = totalCount;
            Items = items;
        }
    } 
}
